import { createRouter, createWebHistory } from 'vue-router'
import { useUserStore } from '@/stores/user'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      redirect: '/login',
    },
    {
      path: '/login',
      name: 'login',
      component: () => import('@/views/LoginView.vue'),
      meta: { title: '登录', requiresAuth: false },
    },
    // 教师路由
    {
      path: '/teacher',
      component: () => import('@/components/layout/AppLayout.vue'),
      meta: { requiresAuth: true, role: 'teacher' },
      children: [
        {
          path: 'dashboard',
          name: 'TeacherDashboard',
          component: () => import('@/views/DashboardView.vue'),
          meta: { title: '仪表盘', requiresAuth: true, role: 'teacher' },
        },
        {
          path: 'facilities',
          name: 'TeacherFacilities',
          component: () => import('@/views/teacher/FacilitiesView.vue'),
          meta: { title: '设施管理', requiresAuth: true, role: 'teacher' },
        },
        {
          path: 'maintenance',
          name: 'TeacherMaintenance',
          component: () => import('@/views/teacher/MaintenanceView.vue'),
          meta: { title: '维修管理', requiresAuth: true, role: 'teacher' },
        },
        {
          path: 'Statistics',
          name: 'TeacherStatistics',
          component: () => import('@/views/teacher/StatisticsView.vue'),
          meta: { title: '统计分析', requiresAuth: true, role: 'teacher' },
        },
        {
          path: 'questionAnalysis',
          name: 'TeacherQuestionAnalysis',
          component: () => import('@/views/teacher/QuestionAnalysisView.vue'),
          meta: { title: '智能问题分析', requiresAuth: true, role: 'teacher' },
        },
      ],
    },
    // 学生路由
    {
      path: '/student',
      component: () => import('@/components/layout/AppLayout.vue'),
      meta: { requiresAuth: true, role: 'student' },
      children: [
        {
          path: 'dashboard',
          name: 'StudentDashboard',
          component: () => import('@/views/DashboardView.vue'),
          meta: { title: '仪表盘', requiresAuth: true, role: 'student' },
        },
        {
          path: 'findFacilities',
          name: 'StudentFacilities',
          component: () => import('@/views/student/FindFacilitiesView.vue'),
          meta: { title: '设施查询', requiresAuth: true, role: 'student' },
        },
      ],
    },
    // 404路由
    {
      path: '/:pathMatch(.*)*',
      redirect: '/login',
    },
  ],
})

// 路由守卫
router.beforeEach((to, _from, next) => {
  const userStore = useUserStore()
  const requiresAuth = to.meta.requiresAuth
  const role = to.meta.role

  // 设置页面标题
  document.title = `${to.meta.title || '校园设施管理系统'}`

  // 不需要认证的页面直接通过
  if (!requiresAuth) {
    // 如果已登录且访问登录页，重定向到对应角色的首页
    if (to.path === '/login' && userStore.isLoggedIn) {
      if (userStore.userInfo?.role === 'teacher') {
        next('/teacher/dashboard')
      } else {
        next('/student/dashboard')
      }
    } else {
      next()
    }
    return
  }

  // 需要认证但未登录，重定向到登录页
  if (!userStore.isLoggedIn) {
    next('/login')
    return
  }

  // 已登录但角色不匹配，重定向到对应角色的首页
  if (role && userStore.userInfo?.role !== role) {
    if (userStore.userInfo?.role === 'teacher') {
      next('/teacher/dashboard')
    } else {
      next('/student/dashboard')
    }
    return
  }

  next()
})

export default router
